package com.yonglin.mvvmapplication.view

import android.os.Bundle

import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.yonglin.mvvmapplication.R
import com.yonglin.mvvmapplication.model.AccountBean
import com.yonglin.mvvmapplication.viewmodel.AccountViewModel
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {
    private lateinit var mAccountViewModel: AccountViewModel


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        supportFragmentManager.beginTransaction().add(R.id.container,FragmentOne()).commit()
        supportFragmentManager.beginTransaction().add(R.id.container2,FragmentTwo()).commit()

        mAccountViewModel = ViewModelProviders.of(this).get(AccountViewModel::class.java)
        change_btn.setOnClickListener {
            mAccountViewModel.getAccount().postValue(AccountBean("zhang", "13420001333"))
        }

        mAccountViewModel.getAccount().observe(this, Observer {
            textview.text = it.name + ": " + it.telPhone
        })
        //当数据变化发生时,调用该方法
        NetWorkLiveData.getInstance(this).observe(this, Observer {
            Toast.makeText(this," "+it.typeName,Toast.LENGTH_SHORT).show()
        })


    }

}
